package cn.csust.net2.manager.client;

import cn.csust.net2.manager.client.app.App;
import cn.csust.net2.manager.shared.service.LoginService;
import cn.csust.net2.manager.shared.service.LoginServiceAsync;
import cn.csust.net2.manager.shared.util.Utils;
import cn.csust.net2.manager.shared.vo.LoginVo;

import com.extjs.gxt.themes.client.Access;
import com.extjs.gxt.themes.client.Slate;
import com.extjs.gxt.ui.client.util.ThemeManager;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.Window;
/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Manager implements EntryPoint {
	 public static boolean isExplorer() {
		    String test = Window.Location.getPath();
		    if (test.indexOf("pages") != -1) {
		      return false;
		    }
		    return true;
		  }
	public void onModuleLoad() {
		LoginServiceAsync service = GWT.create(LoginService.class);
		themeLoad();
		// 调用testLogin(检查是否有用户已经登陆)

		// 1.在Session中有USER
		// 返回LoginVO
		// 生成界面
		// 2.在Session中无USER
		// 返回空的USER生成登陆框
		// 加载LoginVO(将用户写进session中)
		// 生成界面
		final HandlerManager eventBus = new HandlerManager(this);
		service.testLogin(new AsyncCallback<LoginVo>() {

			@Override
			public void onSuccess(LoginVo result) {
				// 连接成功
				if (result.isLogin()) {
					// 已经登录
					App.user = result.getUser();
					App app = new App(eventBus, Utils.changeToMap(result.getAuthorities()));
					RootPanel.get().add(app);
				} else {
					// 还没登陆
					RootPanel.getBodyElement().addClassName("backgroundImage");
					Login login = new Login(eventBus);
					login.setStyleAttribute("opacity", "0.7");
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				// 连接失败
				MessageBox.alert("警告", "网络连接失败，请稍后再试", null); 
			}
		});
	}

	/**
	 * 加载主题信息
	 */
	private void themeLoad() {
		ThemeManager.register(Slate.SLATE);
		ThemeManager.register(Access.ACCESS);
	}

}
